package com.ruoyi.quanzi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.quanzi.domain.SunTopic;
import com.ruoyi.quanzi.domain.SunTopicMember;
import com.ruoyi.quanzi.service.ISunTopicService;
import com.ruoyi.quanzi.service.ISunTopicMemberService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 话题管理Controller
 * 
 * @author ruoyi
 * @date 2024-12-19
 */
@RestController
@RequestMapping("/system/topic")
public class SunTopicController extends BaseController
{
    @Autowired
    private ISunTopicService sunTopicService;
    
    @Autowired
    private ISunTopicMemberService sunTopicMemberService;

    /**
     * 查询话题列表
     */
    @PreAuthorize("@ss.hasPermi('system:topic:list')")
    @GetMapping("/list")
    public TableDataInfo list(SunTopic sunTopic)
    {
        startPage();
        List<SunTopic> list = sunTopicService.selectSunTopicList(sunTopic);
        return getDataTable(list);
    }

    /**
     * 导出话题列表
     */
    @PreAuthorize("@ss.hasPermi('system:topic:export')")
    @Log(title = "话题管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SunTopic sunTopic)
    {
        List<SunTopic> list = sunTopicService.selectSunTopicList(sunTopic);
        ExcelUtil<SunTopic> util = new ExcelUtil<SunTopic>(SunTopic.class);
        util.exportExcel(response, list, "话题数据");
    }

    /**
     * 获取话题详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:topic:query')")
    @GetMapping(value = "/{topicId}")
    public AjaxResult getInfo(@PathVariable("topicId") Long topicId)
    {
        return AjaxResult.success(sunTopicService.selectSunTopicById(topicId));
    }

    /**
     * 获取话题成员统计
     */
    @PreAuthorize("@ss.hasPermi('system:topic:query')")
    @GetMapping("/{topicId}/members/stats")
    public AjaxResult getMemberStats(@PathVariable Long topicId)
    {
        int memberCount = sunTopicMemberService.countTopicMembers(topicId);
        return AjaxResult.success().put("memberCount", memberCount);
    }

    /**
     * 修改话题
     */
    @PreAuthorize("@ss.hasPermi('system:topic:edit')")
    @Log(title = "话题管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SunTopic sunTopic)
    {
        return toAjax(sunTopicService.updateSunTopic(sunTopic));
    }

    /**
     * 删除话题
     */
    @PreAuthorize("@ss.hasPermi('system:topic:remove')")
    @Log(title = "话题管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{topicIds}")
    public AjaxResult remove(@PathVariable Long[] topicIds)
    {
        return toAjax(sunTopicService.deleteSunTopicByTopicIds(topicIds));
    }

    /**
     * 修改话题状态
     */
    @PreAuthorize("@ss.hasPermi('system:topic:edit')")
    @Log(title = "话题状态", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SunTopic sunTopic)
    {
        return toAjax(sunTopicService.updateSunTopic(sunTopic));
    }

    /**
     * 查询话题成员列表
     */
    @PreAuthorize("@ss.hasPermi('system:topic:query')")
    @GetMapping("/{topicId}/members")
    public TableDataInfo listMembers(@PathVariable("topicId") Long topicId, SunTopicMember sunTopicMember)
    {
        startPage();
        sunTopicMember.setTopicId(topicId);
        List<SunTopicMember> list = sunTopicMemberService.selectSunTopicMemberList(sunTopicMember);
        return getDataTable(list);
    }

    /**
     * 移除话题成员
     */
    @PreAuthorize("@ss.hasPermi('system:topic:edit')")
    @Log(title = "移除话题成员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{topicId}/members/{userId}")
    public AjaxResult removeMember(@PathVariable("topicId") Long topicId, @PathVariable("userId") Long userId)
    {
        return toAjax(sunTopicMemberService.leaveTopic(topicId, userId));
    }

    /**
     * 设置成员角色
     */
    @PreAuthorize("@ss.hasPermi('system:topic:edit')")
    @Log(title = "设置成员角色", businessType = BusinessType.UPDATE)
    @PutMapping("/{topicId}/members/{userId}/role")
    public AjaxResult setMemberRole(@PathVariable("topicId") Long topicId, 
                                   @PathVariable("userId") Long userId, 
                                   @RequestBody SunTopicMember sunTopicMember)
    {
        // 先查询成员ID
        SunTopicMember member = sunTopicMemberService.selectSunTopicMemberByTopicIdAndUserId(topicId, userId);
        if (member == null) {
            return AjaxResult.error("成员不存在");
        }
        return toAjax(sunTopicMemberService.setMemberRole(member.getId(), sunTopicMember.getRole()));
    }

    /**
     * 禁言/解除禁言成员
     */
    @PreAuthorize("@ss.hasPermi('system:topic:edit')")
    @Log(title = "成员禁言操作", businessType = BusinessType.UPDATE)
    @PutMapping("/{topicId}/members/{userId}/mute")
    public AjaxResult toggleMemberMute(@PathVariable("topicId") Long topicId, 
                                      @PathVariable("userId") Long userId, 
                                      @RequestBody SunTopicMember sunTopicMember)
    {
        // 先查询成员ID
        SunTopicMember member = sunTopicMemberService.selectSunTopicMemberByTopicIdAndUserId(topicId, userId);
        if (member == null) {
            return AjaxResult.error("成员不存在");
        }
        
        if (sunTopicMember.getMuteEndTime() != null) {
            // 禁言
            member.setMuteEndTime(sunTopicMember.getMuteEndTime());
            member.setStatus(1); // 设置为禁言状态
            return toAjax(sunTopicMemberService.updateSunTopicMember(member));
        } else {
            // 解除禁言
            return toAjax(sunTopicMemberService.unmuteMember(member.getId()));
        }
    }
}